package com.project.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import javax.jws.soap.SOAPBinding.Use;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.exception.ConstraintViolationException;

import com.project.dao.UserDao;
import com.project.util.Fileupload;
import com.project.util.StringUtil;
import com.project.vo.Validator;
import com.project.vo.Admin;
import com.project.vo.Picture;
import com.project.vo.Product;
import com.project.vo.User;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class UserDaoImpl implements UserDao {

	private SessionFactory sessionFactory;
	public Session session = null;
	private Product product = new Product();

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	@Override
	public User login(Integer userId, String password) {
		// TODO Auto-generated method stub
		List<User> users = null;
		User user = new User();
		session = sessionFactory.openSession();
		String hql = "from User user where user.id = ? and user.password = ?";
		try {
			Query query = session.createQuery(hql);
			query.setParameter(0, userId);
			query.setParameter(1, password);
			users = query.list();
			if(!users.isEmpty()){
				user = users.get(0);
			}
		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user;
	}

	@Override
	public Admin login_A(Integer id, String password) {
		// TODO Auto-generated method stub
		List<Admin> admins = null;
		Admin admin = new Admin();
		session = sessionFactory.openSession();
		String hql = "from Admin admin where admin.id = ? and admin.password = ?";
		try {
			Query query = session.createQuery(hql);
			query.setParameter(0, id);
			query.setParameter(1, password);
			admins = query.list();
			if(!admins.isEmpty()){
				admin = admins.get(0);
			}
		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			session.close();
		}
		return admin;
	}
	@Override
	public String updatePassword(Integer userId, String password) {
		// TODO Auto-generated method stub
		String result = StringUtil.FAILURE;
		User user = getUserById(userId);
		user.setPassword(password);
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			session.update(user);
			session.getTransaction().commit();
			result = StringUtil.SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			session.close();
		}
		return result;
	}
	
	private User getUserById(Integer userId) {
		// TODO Auto-generated method stub
		User user = new User();
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			user = (User)session.get(User.class, userId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user;
	}

	public User getUserByName(String username) {
		List<User> users = new ArrayList<User>();
		User user = new User();
		session = sessionFactory.openSession();
		String hql = "from User user where user.name = ?";
		try {
			Query query = session.createQuery(hql);
			query.setParameter(0, username);
			users = query.list();
			if(!users.isEmpty()){
				user = users.get(0);
			}
		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user;
	}

	@Override
	public String register(User user) {
		// TODO Auto-generated method stub
		String result = StringUtil.FAILURE;
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			session.save(user);
			session.getTransaction().commit();
			result = StringUtil.SUCCESS;
		}catch (ConstraintViolationException e) {
			// TODO: handle exception
			result = StringUtil.E_DUPLICATENAME;
		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			session.close();
		}
		return result;
	}

	@Override
	public String update(Integer userOldId, User user) {
		// TODO Auto-generated method stub
		User olduser = getUserById(userOldId);
		olduser.setName(user.getName());
		olduser.setPhone(user.getPhone());
		olduser.setEmail(user.getEmail());
		olduser.setDescription(user.getDescription());
		olduser.setIDCard(user.getIDCard());
//		String hql = "update User user set user.name = ?, user.phone = ? , user.email = ?, user.descripton = ? where user.name = ?";
		String result = StringUtil.FAILURE;
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			session.update(olduser);		//杩欓儴鍒嗗彲鑳戒細鎶婄敤鎴锋墍鏈夌殑浜у搧淇℃伅鍐叉帀
			session.getTransaction().commit();
			result = StringUtil.SUCCESS;
		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			session.close();
		}
		return result;
	}

	@Override
	public User view(Integer userId) {
		// TODO Auto-generated method stub
		User user = new User();
		user = getUserById(userId);
		return user;
	}
	@Override
	public Admin view_A(Integer id) {
		// TODO Auto-generated method stub
		Admin admin = new Admin();
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			admin = (Admin)session.get(Admin.class, id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			session.close();
		}
		return admin;
	}
	@Override
	public User findUserByEmail(String email) {
		// TODO Auto-generated method stub
		List<User> users = new ArrayList<User>();
		User user = new User();
		session = sessionFactory.openSession();
		String hql = "from User user where user.email = ?";
		try {
			Query query = session.createQuery(hql);
			query.setParameter(0, email);
			users = query.list();
			if(!users.isEmpty()){
				user = users.get(0);
			}
		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user;
	}
	@Override
	public String saveCode(String email, String code) {
		// TODO Auto-generated method stub
		String result = StringUtil.FAILURE;
		try {
			Validator validator = new Validator();
			validator.setEmail(email);
			validator.setCode(code);
			session = sessionFactory.openSession();
			session.beginTransaction();
			session.saveOrUpdate(validator);
			session.getTransaction().commit();
			result = StringUtil.SUCCESS;
		}catch (ConstraintViolationException e) {
			// TODO: handle exception
			result = StringUtil.E_DUPLICATENAME;
		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			session.close();
		}
		return result;
	}

	@Override
	public Validator findValidatorByEmail(String email) {
		// TODO Auto-generated method stub
		List<Validator> validators = new ArrayList<Validator>();
		Validator validator = new Validator();
		try {
			session = sessionFactory.openSession();
			String hql = "from Validator validator where validator.email = ?";
			Query query = session.createQuery(hql);
			query.setParameter(0, email);
			validators = query.list();
			if(!validators.isEmpty()){
				validator = validators.get(validators.size()-1);
			}
		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			session.close();
		}
		return validator;
	}
	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	@Override
	public List<User> getAllUser(Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		List<User> users = new ArrayList<User>();
		session = sessionFactory.openSession();
		String hql = "from User user";
		try {
			Query query = session.createQuery(hql);
			query.setFirstResult((pageNum-1)*pageSize);
			query.setMaxResults(pageSize);
			users = query.list();
		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			session.close();
		}
		return users;
	}
	@Override
	public List<User> getAllUser() {
		// TODO Auto-generated method stub
		List<User> users = new ArrayList<User>();
		session = sessionFactory.openSession();
		String hql = "from User user";
		try {
			Query query = session.createQuery(hql);
			users = query.list();
		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			session.close();
		}
		return users;
	}

	@Override
	public String delete(Integer id) {
		// TODO Auto-generated method stub
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			User user = (User)session.get(User.class, id);
			session.delete(user);
			session.getTransaction().commit();
			return StringUtil.SUCCESS;
		} catch (HibernateException e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
			return StringUtil.FAILURE;
		} finally {
			session.close();
		}
	}
}
